﻿using System;
using System.Windows.Forms;
using QuickTest.Core.Events;
using QuickTest.Core.TestRunner;

namespace QuickTest.UI.Controls
{
    /// <summary>
    /// The TestRunnerControl provides the test runner interface.
    /// </summary>
    public partial class TestRunnerControl : UserControl, IRunsTests
    {
        public TestRunnerControl()
        {
            InitializeComponent();
        }

        public void Initialize(IEventHub eventHub)
        {
            testOutputTextBox.RegisterEvents(eventHub);
            testProgressBar.RegisterEvents(eventHub);
        }

        public void RunTests(ITestLoader testLoader, ITestEvents testEvents)
        {
            if (testLoader == null)
                throw new ArgumentNullException("testLoader");

            if (testEvents == null)
                throw new ArgumentNullException("testEvents");

            ExecuteTestRun(testLoader, testEvents);
        }

        private void ExecuteTestRun(ITestLoader testLoader, ITestEvents testEvents)
        {
            using (testLoader)
            {
                testLoader.Run<NUnitTestRunner>(testEvents);
            }
        }
    }
}